<?php

namespace Rblog\Module\Admin\Controller;

class Artsort extends Index {
    private $_model;
    
    public function __construct() {
        $this->_working = 'artsort';
        parent::__construct();
        $this->_model = \Rexzy::model( 'Artsort' );
        
    }
    
    /**
     * 显示分类列表
     */
    public function show() {
        $sorturl = \Rexzy::url( 'admin/artsort' );
        $addurl = \Rexzy::url( 'admin/asortadd' );
        $editurl = \Rexzy::url( 'admin/asortedit' );
        $this->view->assign( array( 'sorturl' => $sorturl,
                'addurl' => $addurl,
                'editurl' => $editurl ) );
        
        if ( $this->request->input('parent', 'g' ) !== false ) {
            $parent = $this->request->input('parent', 'g' );
            $where = array( 'sortid' => $parent );
            $parentName = $this->_model->select( 'name', $where )['name'];
            $this->view->assign( 'parentName', $parentName );
            
        } else {
            $parent = 0;
        }
        
        if ( $parent != 0 ) {
            $where = array( 'sortid' => $parent );
            $grdPar = $this->_model->select( 'parent', $where )['parent'];
            if ( $grdPar != 0 ) {
                $where = array( 'sortid' => $grdPar );
                $grdName = $this->_model->select( 'name', $where )['name'];
            } else {
                $grdName = '';
            }
            $this->view->assign( array( 
                'grdPar' => $grdPar,
                'grdName' => $grdName ) );
        }
        
        $where = array( 'parent' => $parent );
        $sort = $this->_model->selectAll( '*', $where );
        $arts = \Rexzy::model( 'Arts' );
        foreach ( $sort as $key => $val ) {
            $where = 'where find_in_set(?, sort)';
            $params = array( $val['sortid'] );
            $sort[$key]['arts'] = $arts->select( 'count(*)', $where, '', $params )['count(*)'];
        }
        
        $this->view->assign( array( 
            'sort' => $sort,
            'parent' => $parent ) );
        $this->view->display();
    }
    
    /**
     * 增加分类
     */
    public function add() {
        if ( $this->request->isGet() == true ) {
            if ( $this->request->input( 'parent', 'g') !== false ) {
                $sortid = $this->request->input( 'parent', 'g');
                $sortMesg = $this->_model->select('name, children, parent', "where sortid = $sortid");
                
                if ( $sortMesg['children'] == 0 ) {
                    if ( $sortMesg['parent'] == 0 ) {
                        $back = 0;
                        $backName = '';
                    } else {
                        $back = $sortMesg['parent'];
                        $where = array( 'sortid' => $sortMesg['parent'] );
                        $backName = $this->_model->select('name', $where)['name'];
                    }
                } else {
                    $back = $sortid;
                    $backName = $sortMesg['name'];
                }
                $this->view->assign( array( 
                    'sortid' => $sortid,
                    'sortMesg' => $sortMesg
                    ) );
            } else {
                $back = 0;
                $backName = '';
            }
           
            $this->view->assign( array( 
                'sortData' => $this->_model,
                'backName' => $backName,
                'back' => $back,
                ) );
            $this->view->display();
            
        } else if ( $this->request->isPost() == true ) {
            $post = $this->request->allInput('p');
            if ( $post['slugname'] == ''){
                $post['slugname'] = $post['name'];
            }
            
            $name = array(
                'val' => $post['name'],
                'message' => '请输入分类名称',
                'rules' => array(
                    array(
                        'rule' => function( $val ) {
                                $result = $this->_model->check('name',$val);
                                if ( $result !== false ) {
                                    return false;
                            }
                        },
                        'error' => '该分类名已经存在'
                    )
                )
            );
            
            $slugname = array(
                'val' => $post['slugname'],
                'message' => '请输入分类缩略名',
                'rules' => array(
                    array(
                        'rule' => function( $val ) {
                            $result = $this->_model->check( 'slugname', $val );
                            if ( $result !== false ) {
                                    return false;
                            }
                        },
                        'error' => '缩略名已存在'
                    )
                )
            );
            
            $parent = array(
                'val' => $post['parent'],
            );
            
            $description = array(
                'val' => $post['description'],
                'require' => false,
            );
            
            $data = array( 
                'name' => $name,
                'slugname' => $slugname,
                'parent' => $parent,
                'description' => $description 
            );
            $add = function( $val ){
                return $this->_model->add( $val['name'], $val['slugname'], 
                        $val['parent'], $val['description']);
            };
            
            echo json_encode( $this->formCheck( $data, $add ) );
        }
    }
    
    /**
     * 编辑分类信息
     */
    public function edit() {
        if ( $this->request->isGet() == true ) {
            $sortid = $this->request->input( 'sortid', 'g' );
            $where = array( 'sortid' => $sortid );
            $sortMesg = $this->_model->select( '*', $where );
            if ( $sortMesg['parent'] != 0 ) {
                $where = array( 'sortid' => $sortMesg['parent'] );
                $parentName = $this->_model->select( '*', $where )['name'];
            } else {
                $parentName = '';
            }
            $this->view->assign( array( 
                'sortMesg' => $sortMesg,
                'sortData' => $this->_model,
                'parent' => $sortMesg['parent'],
                'parentName' => $parentName,
                ) );
            $this->view->display();
            
        } else if ( $this->request->isPost() == true ) {
            $post = $this->request->allInput( 'p' );
            if ( $post['slugname'] == ''){
                $post['slugname'] = $post['name'];
            }
            $sortid = $post['sortid'];
            $where = array( 'sortid' => $sortid );
            $originalPar = $this->_model->select( '*', $where )['parent'];
            
            $name = array(
                'val' => $post['name'],
                'message' => '请输入分类名称',
                'rules' => array(
                    array(
                        'rule' => function( $val )use( $sortid ) {
                            $result = $this->_model->check('name',$val);
                            if ( ($result !== false ) && ( $result != $sortid ) ) {
                                return false;
                            }
                        },
                        'error' => '该分类名已经存在'
                    )
                )
            );
            
            $slugname = array(
                'val' => $post['slugname'],
                'message' => '请输入分类缩略名',
                'rules' => array(
                    array(
                        'rule' => function( $val )use($sortid) {
                            $result = $this->_model->check( 'slugname', $val );
                            if ( ($result !== false ) && ( $result != $sortid ) ) {
                                return false;
                            }
                        },
                        'error' => '缩略名已存在'
                    )
                )
            );
            
            $parent = array(
                'val' => $post['parent'],
            );
            
            $description = array(
                'val' => $post['description'],
                'require' => false,
            );
            
            $data = array( 
                'name' => $name,
                'slugname' => $slugname,
                'parent' => $parent,
                'description' => $description 
            );
            
            $update = function( $val )use( $sortid, $originalPar ) {
                $where = "sortid = '$sortid'";
                $result = $this->_model->update( $val, $where );
                
                if ( ( $originalPar != 0 ) && ( $val['parent'] != $originalPar ) ) {
                    $minus = $this->_model->childChange( $originalPar, '- 1' );
                }
                if ( ( $val['parent'] != $originalPar ) && ( $val['parent'] != 0 ) ){
                    $add = $this->_model->childChange( $val['parent'], '+ 1' );
                }
                return true;
            };
            
            echo json_encode( $this->formCheck( $data, $update ) );
        }
        
    }
    
    /**
     * 删除分类操作
     */
    public function delete() {
        $postVal = $this->request->input( 'value', 'p');
        $sortid = explode( ',', $postVal );
        
        $result = $this->_model->delsort( $sortid );
        
        if ( $result > 0 ) {
            echo json_encode( array( 'delresult' => true ) );
        } else {
            echo json_encode( array( 'delresult' => false ) );
        }
    }
}

